package com.example.MyRESTAssured.restAsurred;

import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import io.restassured.path.json.JsonPath;
import io.restassured.path.xml.XmlPath;

import io.restassured.response.Response;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static io.restassured.RestAssured.*;
import static io.restassured.http.ContentType.TEXT;
import static io.restassured.path.json.JsonPath.from;
//import static io.restassured.path.xml.XmlPath.from;
import static org.hamcrest.CoreMatchers.*;

public class GetResponseDataTest {

    @Test
    public void TestvalidateResponseDataGetRequest() throws IOException {
        byte[] byteArray = get("/lotto").asByteArray();
        System.out.println(byteArray);

        String json = get("/lotto").asString();
        System.out.println(json);

        InputStream stream = get("/lotto").asInputStream();
        System.out.println(stream.toString());
        stream.close();
        System.out.println("验证将返回数据转化成各种数据成功！");
    }

    /**
     * 从返回值中抽取url,然后继续下一次验证
     * @return
     */
    @Test
    public void TestvalidateUrl2UrlGetRequest(){
        RestAssured.registerParser("text/plain", Parser.JSON);
        String nextTitleLink = given().
                                    param("page", 1).
                                when().
                                    get("/title").
                                then().
                                    contentType(TEXT).
                                    body("title", equalTo("My Title")).
                                extract().
                                    path("_links.next.href");
        System.out.println(nextTitleLink);

        Response response = given().
                                param("page", "5").
                            when().
                                get("/title").
                            then().
                                contentType(TEXT).
                                body("title", equalTo("My Title")).
                            extract().
                                response();

        nextTitleLink = response.path("_links.next.href");
        String headerValue = response.header("headerName");
        System.out.println(nextTitleLink+":"+headerValue);

        System.out.println("从json中抽取数据！");
    }

    /**
     * 使用jsonPath从结果值抽取属性
     * @return
     */
    @Test
    public void TestvalidateJsonPathGetRequest(){

        String json = get("/lotto").asString();
        int lottoId = from(json).getInt("lotto.lottoId");
        System.out.println(lottoId);
        List<Integer> winnerIds = from(json).get("lotto.winners.winnerId");
        System.out.println(winnerIds);

        JsonPath jsonPath = new JsonPath(json).setRoot("lotto");
        lottoId = jsonPath.getInt("lottoId");
        System.out.println(lottoId);
        winnerIds = jsonPath.get("winners.winnerId");
        System.out.println(winnerIds);

        //jsonPath配置
//        JsonPath jsonPath = new JsonPath(json).using(new JsonPathConfig("UTF-8"));
//        JsonPath.config = new JsonPathConfig("UTF-8");

        System.out.println("使用jsonPath从json中抽取数据！");
    }

    /**
     * 使用XMLPath从结果值抽取属性
     * xml和json的from()只能二选一，不能同时存在
     * @return
     */
    @Test
    public void TestvalidateXMLPathGetRequest(){

        RestAssured.registerParser("text/plain",Parser.XML);
        String xml = post("/greetXML?firstName=John&lastName=Doe").andReturn().asString();

//        String firstName = from(xml).get("greeting.firstName");
//        String lastName = from(xml).get("greeting.firstName");
//        System.out.println(firstName+":"+lastName);

        XmlPath xmlPath = new XmlPath(xml).setRoot("greeting");
        String firstName = xmlPath.get("firstName");
        String lastName = xmlPath.get("lastName");
        System.out.println(firstName+":"+lastName);

        //配置xmlPATH,反序列化
//        XmlPath xmlPath = new XmlPath(xml).using(new XmlPathConfig("UTF-8"));
//        XmlPath.config = new XmlPathConfig("UTF-8");

        System.out.println("使用xmlPath从json中抽取数据！");
    }

    /**
     * 使用xmlPath解析html
     */
    @Test
    public void TestvalidateParseHTMLGetRequest(){

        String html ="<html>\n" +
                "<head>\n" +
                "    <title>my title</title>\n" +
                "  </head>\n" +
                "  <body>\n" +
                "    <p>paragraph 1</p>\n" +
                "     <br>\n" +
                "    <p>paragraph 2</p>\n" +
                "  </body>\n" +
                "</html>";
        XmlPath xmlPath = new XmlPath(XmlPath.CompatibilityMode.HTML, html);
        String res=xmlPath.getString("html.head.title");
        System.out.println(res);

        System.out.println("使用xmlPath解析html成功！");
    }




}
